﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Project.EPMAuditing.Library;
using System.Globalization;
using System.IO;

namespace Microsoft.Office.Project.EPMAuditing.AuditListenerCatalog
{
    public class SharePointLogAuditListener : AuditListener
    {
        public SharePointLogAuditListener()
        {
            Microsoft.Office.Server.Diagnostics.PortalLog.ShouldLog(Microsoft.Office.Server.Diagnostics.PortalLogLevel.Verbose);
            ULS.RegisterTraceProvider();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Release managed resources.
            }
            // Release unmanaged resources.
            ULS.UnregisterTraceProvider();

            // Set large fields to null.

            // Call Dispose on your base class.
            base.Dispose(disposing);
        }

        public override void Write(MemoryStream ms)
        {
            // Write the data to the file
            //ChangeRecord changeRecord = DeserializeMessage(ms);
            ChangeRecord changeRecord = new ChangeRecord(ms);
            changeRecord.TracingService = TracingService;

            Microsoft.Office.Server.Diagnostics.PortalLog.LogString(
                String.Format(CultureInfo.InvariantCulture, "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}",
                changeRecord.EventTime, changeRecord.EventName, changeRecord.EventSourceName,
                changeRecord.EventContextInfo.IsWindowsUser.ToString(), changeRecord.EventContextInfo.UserName,
                changeRecord.EventContextInfo.UserGuid.ToString(), changeRecord.EventContextInfo.TrackingGuid.ToString(),
                changeRecord.EventContextInfo.SiteGuid.ToString(), changeRecord.EventContextInfo.Lcid,
                changeRecord.PSEventArgsXml, changeRecord.DataSetChanges));

            //string messageTrimmed = message.Replace(Environment.NewLine, string.Empty);

            //ULS.WriteTrace(0, ULS.TraceSeverity.InformationEvent, Guid.Empty, "EPMAuditing.ReceiverService.exe", "EPM Auditing", "Category Name", messageTrimmed);
        }


    }
}
